import Vue from 'vue'

Vue.filter('telEncryption', tel => {
  if(/^[0-9]+$/.test(tel)) {
    tel = tel.toString().substr(0, 3) + '****' + tel.toString().substr(-4)
  }
  return tel
})


Vue.filter('distanceFormat', distance => {
  if(distance < 1000) {
    return distance + 'm'
  }
  distance = distance.toLocaleString().replace(/,/g, '.').substr(0, 4)

  return distance + 'km'
})


Vue.filter('xiegang', arr => {
  let str = ''
  arr.attrs.forEach(item => {
    str += item.value + '/'
  })
  arr.new_specs.forEach(item => {
    str += item.value + '/'
  })
  str = str.replace(/^\/|\/$/, '')
  return str
})

Vue.filter('desc', obj => {
  let str = ''
  if(obj.new_specs.length > 0) {
    obj.new_specs.forEach(item => {
      str += item.value + '/'
    })
  }else {
    str += ''
  }
  if(obj.attrs.length > 0) {
    obj.attrs.forEach(item => {
      str += item.value + '/'
    })
  }else {
    str += ''
  }
  str = str.replace(/^\/|\/$/, '')
  return str
})

Vue.filter('spec', obj => {
  let str = ''
  if(obj.attr&&obj.attrs.length > 0) {
    obj.attrs.forEach(item => {
      str += item.value
    })
  }
  if(obj.new_specs&&obj.new_specs.length > 0) {
    obj.new_specs.forEach(item => {
      str += item.value
    })
  }
  return str
})
